package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.ArrangeRecordDetail;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants.ArrangeRecordType;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.http.ApiRequest;

public class ArrangeRecordDetailExchangeFragment extends BaseProgressFragment {

	@Bind(R.id.id_home_item_record_icon_bg)
	protected LinearLayout recordIconBg;
	
	@Bind(R.id.id_home_item_record_name)
	protected TextView recordName;
	
	@Bind(R.id.id_home_item_record_status)
	protected TextView recordStatus;

	@Bind(R.id.id_home_item_record_time)
	protected TextView recordTime;

	@Bind(R.id.id_home_item_arrange_record_type)
	protected TextView recordType;

	@Bind(R.id.id_home_item_remark)
	protected TextView itemRemark;

	@Bind(R.id.id_home_item_remark_layout)
	protected View itemRemarkLayout;

	@Bind(R.id.id_home_item_from_user_img)
	protected ImageView fromUserImg;

	@Bind(R.id.id_home_item_from_user_name)
	protected TextView fromUserName;

	@Bind(R.id.id_home_item_to_user_img)
	protected ImageView toUserImg;

	@Bind(R.id.id_home_item_to_user_name)
	protected TextView toUserName;
	
//	@Bind(R.id.id_home_item_time)
//	protected TextView itemTime;

//	@Bind(R.id.id_home_item_icon_bg)
//	protected LinearLayout arrangeTypeIconBg;
//
//	@Bind(R.id.id_home_item_icon_img)
//	protected ImageView arrangeTypeTIconImg;
//
//	@Bind(R.id.id_home_item_icon_name)
//	protected TextView arrangeTypeIconName;
//
//	@Bind(R.id.id_home_item_desc)
//	protected TextView itemDesc;
	
	@Bind(R.id.id_arrange_from_layout)
	protected View arrangeFromLayout;
	
	@Bind(R.id.id_arrange_to_layout)
	protected View arrangeToLayout;

	@Bind(R.id.id_home_item_btn_accept)
	protected TextView btnAccept;

	@Bind(R.id.id_home_item_btn_reject)
	protected TextView btnReject;

	@Bind(R.id.id_home_item_btn_cancel)
	protected TextView btnCancel;

	private int circleId;
	private int arrangeRecordId;
	private ArrangeRecordDetail detail;

	@Override
	protected int obtainContentRes() {
		return R.layout.arrange_record_detail_exchange;
	}

	@Override
	protected void initParams() {
		//解析参数
		Bundle args = getArguments();
		circleId = args.getInt("circle_id", 0);
		arrangeRecordId = args.getInt("arrange_record_id", 0);
	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
		
		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());
		
		if(token.isLogin()){
			
			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("circle_id", circleId);
			params.put("arrange_record_id", arrangeRecordId);

			ApiRequest.arrangeRecordDetail.request(this, params);
			
		}

	}

	@OnClick(R.id.id_home_item_btn_accept)
	public void acceptBtn() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			//排班类型
			ArrangeRecordType type = ArrangeRecordType.getType(detail.getType());

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("arrange_id", detail.getId());
			params.put("circle_id", tokenKeeper.getUser().circleId());

			if (ArrangeRecordType.arrange.equals(type)) {
				params.put("type", 1);
			} else if (ArrangeRecordType.exchange.equals(type)) {
				params.put("type", 2);
			} else if (ArrangeRecordType.replace.equals(type)) {
				params.put("type", 3);
			}

			ApiRequest.arrangeAccept.request(this, params);

		}

	}

	@OnClick(R.id.id_home_item_btn_reject)
	public void rejectBtn() {
		
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			//排班类型
			ArrangeRecordType type = ArrangeRecordType.getType(detail.getType());

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("arrange_id", detail.getId());
			params.put("circle_id", tokenKeeper.getUser().circleId());

			if (ArrangeRecordType.arrange.equals(type)) {
				params.put("type", 1);
			} else if (ArrangeRecordType.exchange.equals(type)) {
				params.put("type", 2);
			} else if (ArrangeRecordType.replace.equals(type)) {
				params.put("type", 3);
			}

			ApiRequest.arrangeReject.request(this, params);

		}

	}

	@OnClick(R.id.id_home_item_btn_cancel)
	public void cancelBtn() {
		
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			//排班类型
			ArrangeRecordType type = ArrangeRecordType.getType(detail.getType());

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("arrange_id", detail.getId());
			params.put("circle_id", tokenKeeper.getUser().circleId());

			if (ArrangeRecordType.arrange.equals(type)) {
				params.put("type", 1);
			} else if (ArrangeRecordType.exchange.equals(type)) {
				params.put("type", 2);
			} else if (ArrangeRecordType.replace.equals(type)) {
				params.put("type", 3);
			}

			ApiRequest.arrangeCancel.request(this, params);

		}

	}

	public static void go(Activity act, int circleId, int arrangeRecordId) {
		
			
			Bundle args = new Bundle();
			args.putInt("circle_id", circleId);
			args.putInt("arrange_record_id", arrangeRecordId);
			
			MyPageHelper.arrangeRecordDetailExchange.showMyPage(act, args);
			

	}

	public TextView getItemRemark() {
		return itemRemark;
	}

	public ImageView getFromUserImg() {
		return fromUserImg;
	}

	public TextView getFromUserName() {
		return fromUserName;
	}

	public ImageView getToUserImg() {
		return toUserImg;
	}

	public TextView getToUserName() {
		return toUserName;
	}

//	public LinearLayout getArrangeTypeIconBg() {
//		return arrangeTypeIconBg;
//	}
//
//	public ImageView getArrangeTypeTIconImg() {
//		return arrangeTypeTIconImg;
//	}
//
//	public TextView getArrangeTypeIconName() {
//		return arrangeTypeIconName;
//	}
//	
//	public TextView getItemDesc() {
//		return itemDesc;
//	}
//	
//	public TextView getItemTime() {
//		return itemTime;
//	}
	
	public TextView getRecordName() {
		return recordName;
	}

	public TextView getRecordType() {
		return recordType;
	}

	public TextView getRecordTime() {
		return recordTime;
	}
	
	public TextView getRecordStatus() {
		return recordStatus;
	}

	public LinearLayout getRecordIconBg() {
		return recordIconBg;
	}

	public TextView getBtnAccept() {
		return btnAccept;
	}

	public TextView getBtnReject() {
		return btnReject;
	}

	public TextView getBtnCancel() {
		return btnCancel;
	}

	public View getItemRemarkLayout() {
		return itemRemarkLayout;
	}
	
	public View getArrangeFromLayout() {
		return arrangeFromLayout;
	}

	public View getArrangeToLayout() {
		return arrangeToLayout;
	}

	public void setDetail(ArrangeRecordDetail detail) {
		this.detail = detail;
	}
	
}
